/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class jClass_PhanPost implements Interface_CacThanhPhanDacTa{
    private int _Bai = 1;
    private jClass_Bien _GiaTriTraVe = new jClass_Bien();
    private jClass_TruongHopDacTa[] _CacTruongHop = new jClass_TruongHopDacTa[0];
    private jClass_BieuThucLuongTu _BieuThucLuongTu;
/*    private static String _BieuThucDinhDang = "(" + jClass_TruongHopDacTa.getBieuThucDinhDang() + ")|("+
            "\\(\\s*(\\(\\s*" + jClass_TruongHopDacTa.getBieuThucDinhDang() + "\\s*\\)\\s*\\\\/\\s*)+\\))";
 * */
    private static String _BieuThucDinhDang = "(.*)|("+
            "(\\(\\s*.*\\s*\\)\\s*\\\\/\\s*)+)";

    public jClass_PhanPost (jClass_Bien giaTriTraVe){
        _GiaTriTraVe = giaTriTraVe;
    }
    /**
     * @return the _GiaTriTraVe
     */
    public jClass_Bien getGiaTriTraVe() {
        return _GiaTriTraVe;
    }

    /**
     * @param GiaTriTraVe the _GiaTriTraVe to set
     */
    public void setGiaTriTraVe(jClass_Bien GiaTriTraVe) {
        this._GiaTriTraVe = GiaTriTraVe;
    }
        //private static String _BieuThucDinhDang = "a" + "|"+
              //"\\((" + "b" + "\\\\/)+" + "b" +"\\)";

    // <editor-fold defaultstate="collapsed" desc="getter and setter">
    /**
     * @return the _BieuThucDinhDang
     */
    public static String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }

    // </editor-fold>

    public Boolean jKiemTraChuoiHopDinhDang (String str_ChuoiCanKienTra)    {
        String str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra;
        if (str_ChuoiDaBoSungDauPhanCach.contains("\\/"))
            str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra.substring(0, str_ChuoiCanKienTra.length() - 1) + "\\/)";
        Pattern pat=Pattern.compile(getBieuThucDinhDang());
        Matcher matcher =pat.matcher(str_ChuoiDaBoSungDauPhanCach);
        return matcher.matches();
    }
    public void jCapNhatTuChuoi(String Str_Post) {
        //int i_SoTruongHop = Str_Post.
        if (!jKiemTraChuoiHopDinhDang(Str_Post)){
            String mess = "Chuoi:\n" + Str_Post + "\n khong hop dinh dang cua mot phan Post!!!";
            JOptionPane.showMessageDialog(null, mess);
            throw new ExceptionInInitializerError(Str_Post);
        }
        Str_Post = Str_Post.trim();
        if (Str_Post.startsWith("(")){
            if (Str_Post.contains("\\/")){
                //Str_Post = Str_Post.trim().substring(1, Str_Post.length() - 1);
                String[] Str_CacTruongHop = Str_Post.split("\\\\/");
                _CacTruongHop = new jClass_TruongHopDacTa[Str_CacTruongHop.length];
                for (int i = 0; i < Str_CacTruongHop.length; i++){
                    String str_TruongHop = Str_CacTruongHop[i];
                    str_TruongHop = str_TruongHop.trim();
                    str_TruongHop = str_TruongHop.substring(1, str_TruongHop.length() - 1);
                    jClass_TruongHopDacTa truongHopDacTa = new jClass_TruongHopDacTa();
                    truongHopDacTa.jCapNhatTuChuoi(str_TruongHop);
                    _CacTruongHop[i] = truongHopDacTa;
                }
            }
            else{
                _CacTruongHop = new jClass_TruongHopDacTa[1];
                jClass_TruongHopDacTa truongHopDacTa = new jClass_TruongHopDacTa();
                truongHopDacTa.jCapNhatTuChuoi(Str_Post);
                _CacTruongHop[1] = truongHopDacTa;
            }
        }
        else{
            setBai(2);
            Str_Post = Str_Post.split("=",2)[1];
            Str_Post = Str_Post.trim();
            _BieuThucLuongTu = new jClass_BieuThucLuongTu(_GiaTriTraVe);
            _BieuThucLuongTu.jCapNhatTuChuoi(Str_Post);
        }
        //_CacTruongHop = new jClass_TruongHopDacTa[]
    }

    /**
     * @return the _CacTruongHop
     */
    public jClass_TruongHopDacTa[] getCacTruongHop() {
        return _CacTruongHop;
    }

    /**
     * @param CacTruongHop the _CacTruongHop to set
     */
    public void setCacTruongHop(jClass_TruongHopDacTa[] CacTruongHop) {
        this._CacTruongHop = CacTruongHop;
    }

    public String jToCCode(int DoLui) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String str_Kq = "";
        if (getBai() == 1){
            for (jClass_TruongHopDacTa truongHopDacTa : _CacTruongHop)
                str_Kq += truongHopDacTa.jToCCode(DoLui) + "\n\n";
            str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf("\n\n"));
        }
        else if(getBai() == 2){
            str_Kq = _BieuThucLuongTu.jToCCode(DoLui);
        }
        return str_Kq;
    }

    public String jCSharpCode(int DoLui) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String str_Kq = "";
        if (getBai() == 1){
            for (jClass_TruongHopDacTa truongHopDacTa : _CacTruongHop)
                str_Kq += truongHopDacTa.jToCCode(DoLui) + "\n\n";
            str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf("\n\n"));
        }
        else if(getBai() == 2){
            str_Kq = _BieuThucLuongTu.jToCCode(DoLui);
        }
        return str_Kq;
    }

    public String jVBNetCode(int DoLui) {
         String str_Kq = "";
        for (jClass_TruongHopDacTa truongHopDacTa : _CacTruongHop)
            str_Kq += truongHopDacTa.jVBNetCode(DoLui) + "\n\n";
        str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf("\n\n"));
        return str_Kq;
    }

    /**
     * @return the _Bai
     */
    public int getBai() {
        return _Bai;
    }

    /**
     * @param Bai the _Bai to set
     */
    public void setBai(int Bai) {
        this._Bai = Bai;
    }
}
